<?php

//Tao Lop DOI_TUONG
class Sys_user {

    private $_id;
    private $_username;
    private $_password;
    private $_role;
    private $_isactive;

    public function getId() {
        return $this->_id;
    }

    public function getUsername() {
        return $this->_username;
    }

    public function getPassword() {
        return $this->_password;
    }

    public function getRole() {
        return $this->_role;
    }

    public function getIsactive() {
        return $this->_isactive;
    }

    public function setId($id) {
        $this->_id = $id;
    }

    public function setUsername($username) {
        $this->_username = $username;
    }

    public function setPassword($password) {
        $this->_password = $password;
    }

    public function setRole($role) {
        $this->_role = $role;
    }

    public function setIsactive($isactive) {
        $this->_isactive = $isactive;
    }

    public function setSys_user($row) {
        $this->setId($row->SU_ID);
        $this->setRole($row->SUR_ID);
        $this->setUsername($row->SU_USER_NAME);
        $this->setPassword($row->SU_PASS_WORD);
        $this->setIsactive($row->IS_ACTIVE);
    }

    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `sys_user`
                                ( 
                                `SUR_ID`,
                                 `SU_USER_NAME`,
                                 `SU_PASS_WORD`,
                                 `IS_ACTIVE`)
                    VALUES (
                            :ROLE_ID,
                            :USERNAME,
                            :PASSWORD,
                            :IS_ACTIVE);";
            $parameters = array(
                ":ROLE_ID",
                ":USERNAME",
                ":PASSWORD",
                ":IS_ACTIVE"
            );
            $values = array(
                $this->getRole(),
                $this->getUsername(),
                $this->getPassword(),
                $this->getIsactive());
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_BOOL
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "delete
                    from `sys_user`
                    where `SU_ID` = :ID;";
            $parameters = array(":ID");
            $values = array($this->getId());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `sys_user`
                    SET `SU_ID` = :ID,
                      `SU_USER_NAME` = :USERNAME,
                      `SU_PASS_WORD` = :PASSWORD,
                      `SUR_ID` = :ROLE_ID,
                      `IS_ACTIVE` = :IS_ACTIVE
                    WHERE `ID` = :ID;";
            $parameters = array(
                ":ID", ":USERNAME",
                ":PASSWORD", ":ROLE_ID",
                ":IS_ACTIVE"
            );
            $values = array(
                $this->getId(), $this->getUsername(),
                $this->getPassword(), $this->getRole(),
                $this->getIsactive());
            $types = array(
                PDO::PARAM_INT, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_BOOL
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `SU_ID`,
                        `SUR_ID`,
                        `SU_USER_NAME`,
                        `SU_PASS_WORD`,
                         `IS_ACTIVE`
                      FROM `sys_user`
                      WHERE SU_ID = :ID";
            $parameters = array(":ID");
            $values = array($this->getId());
            $types = array(PDO::PARAM_INT);
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setSys_user($row);
            }
        }
    }

    public function getByUsername($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `SU_ID`,`SUR_ID`,
                         `SU_USER_NAME`,
                                 `SU_PASS_WORD`,
                                 `IS_ACTIVE`
                      FROM `sys_user`
                      WHERE SU_USER_NAME = :USERNAME";
            $parameters = array(":USERNAME");
            $values = array($this->getUsername());
            $types = array(PDO::PARAM_STR);
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setSys_user($row);
            }
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    `SU_ID`,`SUR_ID`
                         `SU_USER_NAME`,
                                 `SU_PASS_WORD`,
                                 `IS_ACTIVE`
                  FROM `sys_user`";
            $rows = $db->executeQuery($sql);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $sys_user = new Sys_user();
                    $sys_user->setId($row->ID);
                    $sys_user->setUsername($row->USERNAME);
                    $sys_user->setPassword($row->PASSWORD);
                    $sys_user->setRole($row->ROLE_ID);
                    $sys_user->setIsactive($row->IS_ACTIVE);

                    $objects[] = $sys_user;
                }
                return $objects;
            }
        }
    }

}
